<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of m_donsinhvien
 *
 * @author Trung
 */
class M_don_sv extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    public function getID() {
        $this->db->select_max('Ma_Don_SV');
        $query = $this->db->get('tbl_don_sv');
        $data = $query->row();
        $id = $data->Ma_Don_SV;

        if (strlen($id) == 0) {
            $id = 'DSV0000';
        }
        $id++;
        return $id;
    }

    public function getAllID($id_sinh_vien) {
        $this->db->where('FK_Ma_SV', $id_sinh_vien);
        $this->db->from('tbl_don_sv');

        $this->db->join('tbl_bieumau', 'Ma_BieuMau=FK_Ma_BieuMau');
        $this->db->order_by('NgayLap', "desc");

        $query = $this->db->get();

        return $query->result();
    }

    public function getAllDon() {
       
        $this->db->where('TrangThai', 1);
        $this->db->join('tbl_sinh_vien', 'Ma_SV=FK_Ma_SV');
        $this->db->join('tbl_bieumau', 'FK_Ma_BieuMau=Ma_BieuMau');
        $query = $this->db->get('tbl_don_sv');
        return $query->result_array();
    }
     public function getDatain($Ma_BieuMau) {
        $this->db->where('Ma_BieuMau', $Ma_BieuMau);
        $this->db->where('TrangThai', 1);
        $this->db->join('tbl_sinh_vien', 'Ma_SV=FK_Ma_SV');
        $this->db->join('tbl_bieumau', 'FK_Ma_BieuMau=Ma_BieuMau');
        $query = $this->db->get('tbl_don_sv');
        return $query->result_array();
    }
    

    public function getLike($key) {
        $this->db->or_like('FK_Ma_SV', $key);
        $this->db->or_like('HoTen_SV', $key);
        $this->db->or_like('Lop_SV', $key);
        $this->db->or_like('NgayLap', $key);
        $this->db->from('tbl_don_sv');
        $this->db->join('tbl_sinh_vien', 'Ma_SV=FK_Ma_SV');
        $this->db->join('tbl_bieumau', 'Ma_BieuMau=FK_Ma_BieuMau');
        $this->db->order_by('NgayLap', "desc");

        $query = $this->db->get();
        return $query->result_array();
    }

    public function getCount($TrangThai) {
        if ($TrangThai != -1) {
            $this->db->where('TrangThai', $TrangThai);
        }
        $query = $this->db->get('tbl_don_sv');

        return $query->num_rows();
    }

    public function getOneID($id_ma_don) {
        $this->db->where('Ma_Don_SV', $id_ma_don);
        $this->db->from('tbl_don_sv');
        $this->db->join('tbl_bieumau', 'Ma_BieuMau=FK_Ma_BieuMau');
        $query = $this->db->get();
        return $query->row_array();
    }
    public function getDonIn($Ma_Don_SV) {
        $this->db->where('Ma_Don_SV', $Ma_Don_SV);
        $this->db->join('tbl_sinh_vien', 'Ma_SV=FK_Ma_SV');
        $this->db->join('tbl_bieumau', 'FK_Ma_BieuMau=Ma_BieuMau');
        $query = $this->db->get('tbl_don_sv');
        return $query->row_array();
    }

    function capnhattrangthai($Ma_Don_SV, $NoiDung_TT, $Muc) {
        $this->db->set('TrangThai', $Muc);
        $this->db->set('GhiChu', $NoiDung_TT);
        $this->db->where('Ma_Don_SV', $Ma_Don_SV);
        $this->db->update('tbl_don_sv');
        $this->db->set('FK_Ma_Don_SV', $Ma_Don_SV);
        $this->db->set('NoiDung_TT', $NoiDung_TT);
        $this->db->set('Muc', $Muc);
        $this->db->insert('tbl_trangthaidon');
    }

    function capnhat1($Ma_Don_SV, $DaIn) {
        $this->db->set('DaIn', $DaIn);
        $this->db->where('Ma_Don_SV', $Ma_Don_SV);
        $this->db->update('tbl_don_sv');
    }

    function getTrangThai($Ma_Don_SV) {
        $this->db->order_by('ThoiGian_TT','desc');
        $this->db->where('FK_Ma_Don_SV', $Ma_Don_SV);
        $query = $this->db->get('tbl_trangthaidon');
        return $query->result_array();
    }

    public function getOneID2($id_ma_don) {
        $this->db->where('Ma_Don_SV', $id_ma_don);
        $this->db->from('tbl_don_sv');
        $this->db->join('tbl_sinh_vien', 'Ma_SV=FK_Ma_SV');
        $this->db->join('tbl_bieumau', 'Ma_BieuMau=FK_Ma_BieuMau');
        $query = $this->db->get();
        $donsv = $query->row_array();
        $this->db->where('FK_Ma_BieuMau', $donsv['FK_Ma_BieuMau']);
        $query2 = $this->db->get('tbl_odulieu');
        $dsodulieu = $query2->result_array();
        $donsv['NoiDung'] = common::StringToArray($donsv['NoiDung']);
        $data['donsv'] = $donsv;
        $data['dsodulieu'] = $dsodulieu;
        $data['dstrangthai'] = $this->getTrangThai($id_ma_don);
        return $data;
    }

    public function getContentID($id_ma_don) {
        $this->db->where('Ma_Don_SV', $id_ma_don);
        $this->db->select('NoiDung');
        $this->db->limit(1);
        $query = $this->db->get('tbl_don_sv');
        $row = $query->row_array();
        return common::StringToArray($row['NoiDung']);
    }

    public function them($FK_Ma_SV, $datainput, $Ma_BieuMau) {

        date_default_timezone_set("Asia/Ho_Chi_Minh");
        $id = $this->getID();
        $this->db->set('Ma_Don_SV', $id);
        $this->db->set('FK_Ma_SV', $FK_Ma_SV);
        $this->db->set('NoiDung', $datainput);
        $this->db->set('FK_Ma_BieuMau', $Ma_BieuMau);
        $this->db->set('TrangThai', 0);
        $this->db->set('GhiChu', 'Hệ đơn đã gửi thành công, hệ thống đang kiểm tra và xác nhận thông tin');
        $this->db->set('NgayLap', date('Y-m-d H:i:s'));
        if ($this->db->insert('tbl_don_sv')) {
            $this->db->set('FK_Ma_Don_SV', $id);
            $this->db->set('NoiDung_TT', 'Hệ đơn đã gửi thành công, hệ thống đang kiểm tra và xác nhận thông tin');
            $this->db->set('Muc', 0);
            $this->db->insert('tbl_trangthaidon');
            return $id;
        } else {
            return FALSE;
        }
    }

    public function deleteOneID($madon) {
        $this->db->where('FK_Ma_Don_SV', $madon);
        $this->db->delete('tbl_trangthaidon');
        $this->db->where('TrangThai <=', 1);
        $this->db->where('Ma_Don_SV', $madon);
        $this->db->delete('tbl_don_sv');
    }
    function laydonin($trangthai,$Ma_BieuMau){
     //   $this->db->where('Ma_BieuMau', $Ma_BieuMau);
        $this->db->where('TrangThai', $trangthai);
        $this->db->join('tbl_sinh_vien', 'Ma_SV=FK_Ma_SV');
        $this->db->join('tbl_bieumau', 'FK_Ma_BieuMau=Ma_BieuMau');
        $query = $this->db->get('tbl_don_sv');
        return $query->result_array();
    }

}
